package com.itdong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itdong.domain.User;
import com.itdong.mapper.UserMapper;
import com.itdong.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> getAllUser() {
        log.info("获取所有的用户信息");
        List<User> users = userMapper.selectList(null);
        return users;
    }

    @Override
    public User getUserByEmail(String email) {
        log.info("查询邮箱为"+email+"的用户");
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("email", email);
        return userMapper.selectOne(userQueryWrapper);
    }

    @Override
    @Transactional
    public int addNormUser(User user) {
        //要先对密码进行加密
        String hashpw = BCrypt.hashpw(user.getPassword(), BCrypt.gensalt());
        user.setPassword(hashpw);
        //添加到用户表
        int insert = userMapper.insert(user);
        //int insert = userMapper.addNormUser(user); //添加的同时收集id
        Long id = user.getId();
        //还要同时绑定角色，和权限

        return insert;
    }
}
